package com.neusoft.neu24.Result;

/**
 *
 * CommonResult 控制器返回通用类型，所有的控制器的handler方法的返回值类型都是CommonResult，但是在
 * Service层完成实例化动作
 * 泛型类 类型T的作用在软件运行时指明携带的对象的类型
 * @author Administrator
 * @version 2024/05/27 13:45
 **/
import com.neusoft.neu24.entity.Member;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class CommonResult<T> {
    private Integer code =200;
    private String msg ="success";
    /**
     *泛型属性 泛型属性在软件运行时指明携带的对象的类型，携带的数据类型
     */
    private T data;

    public CommonResult(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.data = data;
    }
    public CommonResult(ResultCode resultCode, T data,String msg) {
        this.code = resultCode.getCode();
        this.msg = msg;
        this.data = data;
    }

    public static <T> CommonResult<T> success(T result) {
        return new CommonResult<T>(ResultCode.SUCCESS, result);
    }

    public static <T> CommonResult<T> successMsg(T result,String msg) {
        return new CommonResult<T>(ResultCode.SUCCESS, result, msg);
    }


    public static <T> CommonResult<T> failed(ResultCode code) {
        CommonResult result = new CommonResult();
        result.setCode(code.getCode());
        result.setMsg(code.getMsg());
        result.setData(null);
        return result;
    }
}




